package Dao.Impl;

import Dao.UserDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.User;
import utils.DruidJbdcUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zhangyu
 * @date 2021/11/18    21:23
 */
public class UserDaoImpl implements UserDao {
    /**
     * 数据接口查询所有的用户信息,封装List集合
     * @return 返回list列表
     */
    @Override
    public List<User> findAllUser() throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "select * from user";
        List<User> list = qr.query(sql, new BeanListHandler<User>(User.class));
        return list;
    }


    /***
     * 更新用户数据
     * @param user 用户实体
     * @return 影响行数
     * @throws SQLException
     */
    @Override
    public int update(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "update user set username = ? , password = ? , name = ? , telephone = ? , birthday = ? , sex = ? where uid = ?";
        int count = qr.update(sql, user.getUsername(), user.getPassword(), user.getName(), user.getTelephone(),
                user.getBirthday(), user.getSex(), user.getUid());
        return count;
    }


    /***
     * 据接口访问查询用户通过用户id
     * @param uid 用户id
     * @return用户实体类
     * @throws SQLException
     */
    @Override
    public User selectUserById(String uid) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "select * from user where uid = ?";
        User user = qr.query(sql, new BeanHandler<User>(User.class), uid);
        return user;
    }


    /***
     * 根据接口删除用户
     * @param uid
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteUserByUid(String uid) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "delete from user where uid = ?";
        int count = qr.update(sql, uid);
        System.out.println(count);
        return count;
    }

    /***
     * 通过分页查询用户列表数据
     * @param currentPage
     * @param pageSize
     * @return
     * @throws SQLException
     */

    @Override
    public List<User> selectUserByPage(int currentPage, int pageSize) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "select * from user limit ?,?" ;
        List<User> list = qr.query(sql, new BeanListHandler<User>(User.class),
                (currentPage - 1) * pageSize,
                pageSize);
        return list;
    }


    /***
     *查询用户总记录数
     * @return
     * @throws SQLException
     */
    @Override
    public int getTotalCount() throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "select count(uid) from user";
        Object obj = qr.query(sql, new ScalarHandler<>());
        String str = String.valueOf(obj);
        int totalCount = Integer.parseInt(str);
        return totalCount;
    }
}
